package org.csu.gp_store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.csu.gp_store.bo.FactoryBO;
import org.csu.gp_store.entity.FacBusiness;
import org.csu.gp_store.entity.Factory;
import org.csu.gp_store.entity.Product;
import org.csu.gp_store.mapper.FacBusinessMapper;
import org.csu.gp_store.mapper.FactoryMapper;
import org.csu.gp_store.mapper.ProductMapper;
import org.csu.gp_store.service.FacBusinessService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FacBusinessImpl implements FacBusinessService {

    @Autowired
    FacBusinessMapper facBusinessMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    FactoryMapper factoryMapper;

    @Override
    public boolean facBusinessExist(@NotNull Long ProductID,@NotNull Long FactoryID) {
        LambdaQueryWrapper<FacBusiness> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FacBusiness::getFactoryID,FactoryID);
        lambdaQueryWrapper.eq(FacBusiness::getProductID,ProductID);
        return facBusinessMapper.selectList(lambdaQueryWrapper).size()>0;
    }

    @Override
    public int addFactoryBusiness(FacBusiness facBusiness) {
        if(facBusinessExist(facBusiness.getProductID(),facBusiness.getFactoryID())){
            return 0;
        }
        return facBusinessMapper.insert(facBusiness);
    }

    @Override
    public int changeFacBusiness(FactoryBO factoryBO) {
        FacBusiness facBusiness = FactoryBO.toFacBusiness(factoryBO);
        LambdaQueryWrapper<FacBusiness> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FacBusiness::getFactoryID,facBusiness.getFactoryID());
        lambdaQueryWrapper.eq(FacBusiness::getProductID,facBusiness.getProductID());
        return facBusinessMapper.update(facBusiness,lambdaQueryWrapper);
    }

    @Override
    public int deleteFacBusiness(FactoryBO factoryBO) {
        FacBusiness facBusiness = FactoryBO.toFacBusiness(factoryBO);
        LambdaQueryWrapper<FacBusiness> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FacBusiness::getFactoryID,facBusiness.getFactoryID());
        lambdaQueryWrapper.eq(FacBusiness::getProductID,facBusiness.getProductID());
        return facBusinessMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public List<Product> getAllBusiness(@NotNull Long companyID) {
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Product::getCompanyID,companyID);
        List<Product> productList = productMapper.selectList(lambdaQueryWrapper);
        if(productList.size()>0){
            return productList;
        }
        return null;
    }

    public List<Long> getAllFactoryByProductID(@NotNull Long productID){
        LambdaQueryWrapper<FacBusiness> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FacBusiness::getProductID,productID);
        List<FacBusiness> facBusinessList = facBusinessMapper.selectList(lambdaQueryWrapper);
        List<Long> IDList = new ArrayList<>();
        if(facBusinessList.size()>0){
            for(FacBusiness facBusiness:facBusinessList){
                IDList.add(facBusiness.getFactoryID());
            }
        }
        return IDList;
    }

    @Override
    public List<FacBusiness> getFacBusiness(@NotNull Long factoryID) {
        LambdaQueryWrapper<FacBusiness> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FacBusiness::getFactoryID,factoryID);
        List<FacBusiness> facBusinessList = facBusinessMapper.selectList(lambdaQueryWrapper);
        return facBusinessList;
    }

    @Override
    public List<Long> selectFactoryID(@NotNull Long ProductID,@NotNull Long CompanyID){
        LambdaQueryWrapper<Factory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Factory::getCompanyID,CompanyID);
        List<Factory> factoryList = factoryMapper.selectList(lambdaQueryWrapper);
        List<Long> FactoryIDList = new ArrayList<>();
        if(factoryList.size()>0){
            for(Factory factory:factoryList){
                LambdaQueryWrapper<FacBusiness> facBusinessLambdaQueryWrapper = new LambdaQueryWrapper<>();
                facBusinessLambdaQueryWrapper.eq(FacBusiness::getFactoryID,factory.getFactoryID());
                List<FacBusiness> facBusinessList = facBusinessMapper.selectList(facBusinessLambdaQueryWrapper);
                if(facBusinessList.size()>0){
                    for(FacBusiness facBusiness:facBusinessList){
                        if(facBusiness.getProductID().equals(ProductID)){
                            FactoryIDList.add(facBusiness.getFactoryID());
                        }
                    }
                }
            }
        }
        return FactoryIDList;
    }
}
